package com.zhongshengnetwork.rightbe.lang.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.clerk.view.ClerkFragment;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.topgrid.activity.TopGridActivity;
import com.zhongshengnetwork.rightbe.lang.activity.LangPublishActivity;
import com.zhongshengnetwork.rightbe.login.LoginActivity;
import com.zhongshengnetwork.rightbe.wzt.view.AdapterPage;
import com.zhongshengnetwork.rightbe.wzt.view.WZTLatestFragment;

import java.util.ArrayList;
import java.util.List;

import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_BOTTOM;
import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_CENTER;
import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_STRETCH;

public class SimpleHomeFragment extends BaseAnalyticsFragment {

    private View rootView;
    private ViewPager mPaper;
    private TabLayout tabLayout;
    private LinearLayout home_layout;
    private View home_layout_line;
    public MainActivity mainActivity;

    private CareLangFragment careLangFragment;
    private HotLangFragment hotLangFragment;
    private NewLangFragment newLangFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //System.out.print("首页界面创建\n");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.simple_home_layout, container,false);
            tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
            mPaper = (ViewPager)rootView.findViewById(R.id.viewpager);
            //设置主题下的颜色
            home_layout = (LinearLayout) rootView.findViewById(R.id.home_layout);
            home_layout_line = (View) rootView.findViewById(R.id.home_layout_line);
            home_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            home_layout_line.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());

            AdapterPage adapter = new AdapterPage(getActivity().getSupportFragmentManager());
            careLangFragment = new CareLangFragment();
            adapter.addFragment(careLangFragment, "关注");

            hotLangFragment = new HotLangFragment();
            adapter.addFragment(hotLangFragment, "推荐");

            newLangFragment = new NewLangFragment();
            adapter.addFragment(newLangFragment, "最新");

            mPaper.setAdapter(adapter);
            //解决卡顿问题：https://blog.csdn.net/weixin_34023982/article/details/87958336
            mPaper.setOffscreenPageLimit(adapter.getCount());//将viewpager的缓存个数设置为fragment的个数

            //设置tablayout，viewpager上的标题
            tabLayout.setupWithViewPager(mPaper);
            tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.lightGreenColor));
            tabLayout.setSelectedTabIndicatorGravity(INDICATOR_GRAVITY_BOTTOM);
            tabLayout.setTabTextColors(AppThemeUtils.getInstance().getLightTextColor(),Color.parseColor("#99CC00"));
            tabLayout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            mPaper.setCurrentItem(1);
            mPaper.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });

            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    //设置选中时的颜色
                    if (tab.getCustomView() != null){

                    }
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    //设置未选中时的颜色
                    if (tab.getCustomView() != null){

                    }
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            updateCareTip(false);
        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }


    public void updateAfterLogin(){

        if (careLangFragment != null){
            careLangFragment.getFirstData(true);
        }

    }

    public void setFragmentOfIndex(int select){
        mPaper.setCurrentItem(select);
    }

    public void scrollToFirstData(){

        if (mPaper == null ){
            return;
        }

        int select = mPaper.getCurrentItem();

        if (select == 0){
            if (careLangFragment != null){
                careLangFragment.getFirstData(true);
            }
        }else if (select == 1){
            if (hotLangFragment != null){
                hotLangFragment.getFirstData(true);
            }
        }else if (select == 2){
            if (newLangFragment != null){
                newLangFragment.getFirstData(true);
            }
        }

    }


    public void updateCareTip(Boolean visible){
        /*
        if (viewPagerIndicator != null){
            String tip = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.CareUpdateKey,"0");
            if (tip.equals("1") && CommonUtils.isLogin() == true){
                viewPagerIndicator.setCare(true);
            }else {
                viewPagerIndicator.setCare(false);
            }
        }
        */

    }

    public void getNewData(){
        if (mPaper != null && mPaper.getCurrentItem() == 1){
            if (hotLangFragment != null){
                hotLangFragment.getNewData();
            }
        }
    }


}

